<!--********************************************************************
* Copyright© 2000 - 2018 SuperMap Software Co.Ltd. All rights reserved.
*********************************************************************-->
<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title data-i18n="resources.title_distanceMeasure"></title>
    <style type="text/css">
        .editPane {
            position: absolute;
            left: 50px;
            top: 10px;
            text-align: center;
            background: #FFF;
            z-index: 1000;
        }
    </style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div class='panel panel-primary editPane' id='editPane' style="z-index: 99999">
    <div class='panel-heading'>
        <h5 class='panel-title text-center' data-i18n="resources.text_distanceMeasure"></h5>
    </div>
    <div class='panel-body' id='params'>
        <div align='right' class='button-group'>
            <input type='button' id='btn1' class='btn btn-primary' data-i18n="[value]resources.text_input_value_distanceMeasure" onclick="distanceMeasure()"/>
            <input type='button' id='btn2' class='btn btn-primary' data-i18n="[value]resources.text_input_value_clear" onclick="clearFeatures()"/>
        </div>
    </div>
</div>
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" exclude="iclient-classic" src="../../dist/classic/include-classic.js"></script>
<script type="text/javascript">
    var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
    var map,layer, drawLine, lineLayer,
        style = {
            strokeColor: "#304DBE",
            strokeWidth: 2,
            pointerEvents: "visiblePainted",
            fillColor: "#304DBE",
            fillOpacity: 0.8
        },
        url = host + "/iserver/services/map-world/rest/maps/World";
    //新建线矢量图层
    lineLayer = new SuperMap.Layer.Vector("lineLayer");
    //对线图层应用样式style（前面有定义）
    lineLayer.style = style;


    //创建画线控制，图层是lineLayer;这里DrawFeature(图层,类型,属性)；multi:true在将要素放入图层之前是否现将其放入几何图层中
    drawLine = new SuperMap.Control.DrawFeature(lineLayer, SuperMap.Handler.Path, {multi: true});

    /*
     注册featureadded事件,触发drawCompleted()方法
     例如注册"loadstart"事件的单独监听
     events.on({ "loadstart": loadStartListener });
     */
    drawLine.events.on({"featureadded": drawCompleted});
    //定义layer图层，TiledDynamicRESTLayer：分块动态 REST 图层
    layer = new SuperMap.Layer.TiledDynamicRESTLayer("World", url, {
        transparent: true,
        cacheEnabled: true
    }, {maxResolution: "auto"});
    //为图层初始化完毕添加addLayer()事件
    layer.events.on({"layerInitialized": addLayer});
    map = new SuperMap.Map("map", {
        controls: [
            new SuperMap.Control.LayerSwitcher(),
            new SuperMap.Control.ScaleLine(),
            new SuperMap.Control.Zoom(),
            new SuperMap.Control.Navigation({
                dragPanOptions: {
                    enableKinetic: true
                }
            }),
            drawLine]
    });
    function addLayer() {
        map.addLayers([layer, lineLayer]);
        map.setCenter(new SuperMap.LonLat(0, 0), 1.5);
    }
    function distanceMeasure() {
        widgets.alert.clearAlert();
        clearFeatures();
        drawLine.activate();
    }

    //绘完触发事件
    function drawCompleted(drawGeometryArgs) {
        //停止画面控制
        drawLine.deactivate();
        //获得图层几何对象
        var geometry = drawGeometryArgs.feature.geometry,
            measureParam = new SuperMap.REST.MeasureParameters(geometry), /* MeasureParameters：量算参数类。 客户端要量算的地物间的距离或某个区域的面积*/
            myMeasuerService = new SuperMap.REST.MeasureService(url); //量算服务类，该类负责将量算参数传递到服务端，并获取服务端返回的量算结果
        myMeasuerService.events.on({"processCompleted": measureCompleted});

        //对MeasureService类型进行判断和赋值，当判断出是LineString时设置MeasureMode.DISTANCE，否则是MeasureMode.AREA

        myMeasuerService.measureMode = SuperMap.REST.MeasureMode.DISTANCE;

        myMeasuerService.processAsync(measureParam); //processAsync负责将客户端的量算参数传递到服务端。
    }

    //测量结束调用事件
    function measureCompleted(measureEventArgs) {
        var distance = measureEventArgs.result.distance;
        var unit = measureEventArgs.result.unit;
        widgets.alert.showAlert(resources.msg_measureResult + distance + resources.msg_m,true);
    }

    //移除图层要素
    function clearFeatures() {
        lineLayer.removeAllFeatures();
        widgets.alert.clearAlert();
    }

</script>
</body>
</html>
